package com.retail.proxy.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.result.Result;
import com.retail.proxy.entity.vo.AdminProxyUpgradeDetailVo;
import com.retail.core.entity.ParamReq;
import com.retail.proxy.entity.request.AdminProxyUpgradeDetailRequest;
import com.retail.proxy.entity.vo.ProxyMechanismCategoryVo;
import com.retail.proxy.mapper.AdminProxyUpgradeDetailMapper;
import com.retail.proxy.mapper.ProxyMechanismCategoryMapper;
import com.retail.proxy.service.AdminProxyUpgradeDetailService;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.mapper.AdminMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* 晋升明细 Service impl
* @author Administrator
*/
@Service
public class AdminProxyUpgradeDetailServiceImpl implements AdminProxyUpgradeDetailService {

   @Resource
   private AdminProxyUpgradeDetailMapper adminProxyUpgradeDetailMapper;

   @Override
   public AdminProxyUpgradeDetailVo getModel(Long id) {
      return  adminProxyUpgradeDetailMapper.getModel(id);
   }

   @Transactional
   @Override
   public Long save(AdminProxyUpgradeDetailRequest adminProxyUpgradeDetailRequest) {
      adminProxyUpgradeDetailMapper.save(adminProxyUpgradeDetailRequest);
      return adminProxyUpgradeDetailRequest.getId();
   }

   @Transactional
   @Override
   public Long update(AdminProxyUpgradeDetailRequest adminProxyUpgradeDetailRequest) {
      adminProxyUpgradeDetailMapper.update(adminProxyUpgradeDetailRequest);
      return adminProxyUpgradeDetailRequest.getId();
   }

   @Override
   public List<AdminProxyUpgradeDetailVo> listPage(ParamReq<AdminProxyUpgradeDetailRequest> params) {
      params.setStartIndex((params.getCurrent() -1 ) * params.getSize());
      return adminProxyUpgradeDetailMapper.listPage(params);
   }

   @Override
   public Integer countPage(ParamReq<AdminProxyUpgradeDetailRequest> params) {
       return adminProxyUpgradeDetailMapper.countPage(params);
   }

   @Override
   public Integer updateDelFlagById(Long id, Integer delFlag) {
       return adminProxyUpgradeDetailMapper.updateDelFlagById(id, delFlag);
   }

   @Resource
   private AdminMapper adminMapper;

   @Resource
   private ProxyMechanismCategoryMapper proxyMechanismCategoryMapper;
    @Override
    public Result<List<AdminProxyUpgradeDetailVo>> proxyUpgradeDetailList(Long upgradeId) {
      List<AdminProxyUpgradeDetailVo> list = adminProxyUpgradeDetailMapper.proxyUpgradeDetailList(upgradeId);
      list.forEach(l -> {
         AdminVo adminVo = adminMapper.getModel(l.getAdminId());
         l.setAdminName(adminVo.getName());

        ProxyMechanismCategoryVo categoryVo =  proxyMechanismCategoryMapper.getModel(l.getCategoryId());
        if (ObjectUtil.isNotEmpty(categoryVo)) {
           l.setCategoryName(categoryVo.getCategoryName());
        }
      });
      return Result.success(list);
    }
}
